package org.example.practiceexamples.repository;

import org.example.practiceexamples.dox.Lab;
import org.example.practiceexamples.dto.LabAppointment;
import org.example.practiceexamples.mapper.LabAppointmentResultSetExtractor;
import org.springframework.data.jdbc.repository.query.Modifying;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LabRepository extends CrudRepository<Lab, String> {

    @Query("""
        select * from lab
    """)
    List<Lab> findAllLabs();


    @Query("""
        select * from lab where name =:name 
    """)
    Lab findLabByName(String name);

    @Query("""
        select * from lab where id =:id 
    """)
    Lab findLabById(String id);

    @Query("""
        select lab.quantity from lab where name =:name
    """)
    int findQuantityByName(@Param("name") String name);

    @Query("""
        select lab.manage from lab where name =:name
    """)
    String findManageByName(String name);

    @Query("""
        select lab.manage from lab where id =:lid
    """)
    String findManageById(String lid);

    @Query("""
        select lab.state from lab where name = :name
    """)
    int findStateByName(String name);

    @Query("""
        select lab.description from lab where name = :name
    """)
    String findDescriptionByName(String name);

    @Modifying
    @Query("""
        update lab set state = :state where name = :name
    """)
    void updateStateByName(int state,String name);

    @Modifying
    @Query("""
        update lab set manage =:manage where name =:name
    """)
    void updateManageByName(String manage,String name);

    @Modifying
    @Query("""
        update lab set description =:description where name = :name
    """)
    void updateDescriptionByName(String description,String name);

    @Modifying
    @Query("""
        update lab set quantity = :quantity where name =:name
    """)
    void updateQuantityByName(int quantity,String name);

//    @Modifying
//    @Query("""
//
//    """)
//    void updateMessage();

    @Modifying
    @Query("""
        delete from lab where name = :name
    """)
    void deleteLabByName(String name);

    @Modifying
    @Query("""
        delete from lab where id = :id
    """)
    void deleteLabById(String id);

    @Query(value = """
        select * from lab l join appointment a on l.id = a.lab_id where l.id =:lid
    """,resultSetExtractorClass = LabAppointmentResultSetExtractor.class)
    LabAppointment findLabAppointmentByLid(String lid);

    @Query(value = """
        select * from lab l join appointment a on l.id = a.lab_id where l.name =:lname
    """,resultSetExtractorClass = LabAppointmentResultSetExtractor.class)
    LabAppointment findLabAppointmentByName(String lname);

}
